import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsResponse} from "./GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export interface CreateMarketSaleDocumentRequest {
    note?: string
    year: number
    month: number
}

export function createMarketSaleDocument(marketAccountId: number, request: CreateMarketSaleDocumentRequest) {
    return fetcher.post<number>(`markets/accounts/${marketAccountId}/saleDocuments`, {}, request)
}

export function getMarketSaleDocumentDetail(id: number) {
    return fetcher.get<MarketSaleDocumentDetail>(`markets/accounts/saleDocuments/${id}`, {
        includeItems: true
    })
}

export function getMarketSaleDocumentDetailByMonth(marketAccountId: number, year: number, month: number) {
    return fetcher.get<MarketSaleDocumentDetail>(`markets/accounts/${marketAccountId}/saleDocuments/year/${year}/month/${month}`, {
        includeItems: true
    })
}

export function getMarketSaleDocumentDetailWithoutItems(id: number) {
    return fetcher.get<MarketSaleDocumentDetail>(`markets/accounts/saleDocuments/${id}`, {
        includeItems: true
    })
}

export interface MarketSaleDocumentDetail {
    id: number
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    market: NumberIdAndName
    items: Array<MarketSaleDocumentItem>
    submittedDateTime?: string
    auditedDateTime?: string
    year: number
    month: number
    account: NumberIdAndName
    note?:string
}

export interface MarketSaleDocumentItem extends QueryGoodsResponse {
    amountInOrder: number
    totalFee: number
}

export interface AddMarketSaleDocumentItemRequest {
    amount: number
    goodsId: number
}

export function addMarketSaleDocumentItem(id: number, addMarketSaleDocumentItemRequest: AddMarketSaleDocumentItemRequest) {
    return fetcher.post(`markets/accounts/saleDocuments/${id}/items`, {}, addMarketSaleDocumentItemRequest)
}

export function deleteMarketSaleDocumentItem(orderId: number, goodsId: number) {
    return fetcher.delete(`markets/accounts/saleDocuments/${orderId}/items/${goodsId}`)
}

export interface UpdateMarketSaleDocumentItemAmountRequest {
    amount: number
}

export function updateMarketSaleDocumentItemAmount(orderId: number, goodsId: number, updateMarketSaleDocumentItemAmountRequest: UpdateMarketSaleDocumentItemAmountRequest) {
    return fetcher.patch(`markets/accounts/saleDocuments/${orderId}/items/${goodsId}/amount`, {}, updateMarketSaleDocumentItemAmountRequest)
}

export interface QueryMarketSaleDocumentRequest {
    marketIds: number[]
    startDate?: string
    endDate?: string
    createdByIds: number[]
    status?: MarketSaleDocumentStatus
}

export function queryMarketSaleDocuments(queryMarketSaleDocumentRequest: QueryMarketSaleDocumentRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryMarketSaleDocumentResponse>>("market/saleDocuments", {...queryMarketSaleDocumentRequest, ...pageable})
}

export interface QueryMarketSaleDocumentResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    market: NumberIdAndName
    status: MarketSaleDocumentStatus
    year: number
    month: number
}

export interface QueryMarketSaleDocumentResponseInMarketAccount {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    status: MarketSaleDocumentStatus
    year: number
    month: number
}

export function queryMarketSaleDocumentsByMarketAccountId(accountId: number, pageable: Pageable = new Pageable()) {
    return fetcher.get<Page<QueryMarketSaleDocumentResponseInMarketAccount>>(`markets/accounts/${accountId}/saleDocuments`, {
        ...pageable.toRequestParams(),
    })
}

export type MarketSaleDocumentStatus = keyof typeof marketSaleDocumentStatusMap

export const marketSaleDocumentStatusMap =
    {
        "WAIT_SUBMIT": "待提交",
        "SUBMITTED": "已提交",
        "AUDITED": "已审核",
    } as const
export const marketSaleDocumentStatuses = Object.keys(marketSaleDocumentStatusMap) as MarketSaleDocumentStatus[]

export function getAllMarketSaleDocumentCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("markets/accounts/saleDocuments/createdBys", {projection: "summary"})
}

export function getMarketSaleDocumentCreatedEmployeeByMarketId(id: number) {
    return fetcher.get<Array<NumberIdAndName>>(`markets/${id}/saleDocuments/createdBys`, {projection: "summary"})
}

export function submitMarketSaleDocument(id: number) {
    return fetcher.patch<number>(`markets/accounts/saleDocuments/${id}/submit`)
}

export function deleteMarketSaleDocument(id: number) {
    return fetcher.delete<number>(`markets/accounts/saleDocuments/${id}`)
}

export function validateMarketSaleDocument(id: number) {
    return fetcher.patch<number>(`markets/accounts/saleDocuments/${id}/validate`)
}


export function importMarketSaleDocumentItemsByMonth(marketAccountId: number, year: number, month: number, file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>(`markets/accounts/${marketAccountId}/saleDocuments/year/${year}/month/${month}/items/excel/import`, {}, formData, {
        timeout: 30000
    })
}

export function importMarketSaleDocumentItems(id: number, file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>(`markets/accounts/saleDocuments/${id}/items/excel/import`, {}, formData, {
        timeout: 30000
    })
}


export interface MoveMultiGoodsRequest {
    targetAccountId: number
    goodsIds: number[]
}

export function moveItemsToAnotherDocument(accountId: number, year: number, month: number, request: MoveMultiGoodsRequest) {
    return fetcher.post(`markets/accounts/${accountId}/saleDocuments/year/${year}/month/${month}/goods/move`, {}, request)
}

export function clearItemsByYearAndMonth(accountId: number, year: number, month: number,){
    return fetcher.delete(`markets/accounts/${accountId}/saleDocuments/year/${year}/month/${month}/items`, {})
}